﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YRevitJig
{
	public class XArcJig : XUiJigBase
	{
		public XArcJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.JigStage = XArcJig.EStage.eNoOper;
		}

		public XYZ PickCenterPoint(string strPrompt = "请确定圆弧中心点：")
		{
			try
			{
				this.JigStage = XArcJig.EStage.ePtCenter;
				this.CenterPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.CenterPoint = null;
			}
			this.JigStage = XArcJig.EStage.eNoOper;
			return this.CenterPoint;
		}

		public XYZ PickStartPoint(XYZ ptCenter, string strPrompt = "请确定圆弧起点：")
		{
			try
			{
				this.JigStage = XArcJig.EStage.ePtStart;
				this.CenterPoint = ptCenter;
				this.StartPoint = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch
			{
				this.StartPoint = null;
			}
			this.JigStage = XArcJig.EStage.eNoOper;
			return this.StartPoint;
		}

		public XYZ PickEndPoint(XYZ ptCenter, XYZ ptStart, string strPrompt = "请确定圆弧终点：")
		{
			try
			{
				this.JigStage = XArcJig.EStage.ePtEnd;
				this.CenterPoint = ptCenter;
				this.StartPoint = ptStart;
				this.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				this.EndPoint = this.PointOnCircle(this.EndPoint);
			}
			catch
			{
				this.EndPoint = null;
			}
			this.JigStage = XArcJig.EStage.eNoOper;
			return this.EndPoint;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			bool flag = this.JigStage == XArcJig.EStage.ePtStart;
			if (flag)
			{
				Graphics graphics = paintEventArgs.Graphics;
                System.Drawing.Point point = base.Revit2Client(this.CenterPoint);
				int distance = XDrawable2D.GetDistance(point, this.m_ptCur);
				XCircle2D xcircle2D = new XCircle2D(point, distance);
				XGraphics g = new XGraphics(graphics, base.JigPen);
				xcircle2D.Draw(g);
			}
			else
			{
				bool flag2 = this.JigStage == XArcJig.EStage.ePtEnd;
				if (flag2)
				{
					try
					{
						XYZ xyz = base.Client2Revit(this.m_ptCur);
						xyz = this.PointOnCircle(xyz);
						XYZ ptPick = this.StartPoint + 0.5 * (xyz - this.StartPoint);
						XYZ point2 = this.PointOnCircle(ptPick);
						XArc2D xarc2D = new XArc2D(base.Revit2Client(this.StartPoint), base.Revit2Client(xyz), base.Revit2Client(point2));
						XGraphics g2 = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						xarc2D.Draw(g2);
					}
					catch
					{
					}
				}
			}
		}

		public override List<Curve> CreateCurves()
		{
			List<Curve> list = new List<Curve>();
			try
			{
				bool flag = this.CenterPoint != null && this.StartPoint != null && this.EndPoint != null;
				if (flag)
				{
					XYZ ptPick = this.StartPoint + 0.5 * (this.EndPoint - this.StartPoint);
					XYZ xyz = this.PointOnCircle(ptPick);
					Arc item = Arc.Create(this.StartPoint, this.EndPoint, xyz);
					list.Add(item);
				}
			}
			catch
			{
			}
			return list;
		}

		private XYZ PointOnCircle(XYZ ptPick)
		{
			double num = this.CenterPoint.DistanceTo(this.StartPoint);
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(base.UiDoc.ActiveView.ViewDirection, this.CenterPoint);
			Arc arc = Arc.Create(plane, num, 0.0, Math.PI);
			arc.MakeUnbound();
			return arc.Project(ptPick).XYZPoint;
		}

		public XYZ CenterPoint { get; set; }

		public XYZ StartPoint { get; set; }

		public XYZ EndPoint { get; set; }

		public XArcJig.EStage JigStage { get; set; }

		public enum EStage
		{
			eNoOper,
			ePtCenter,
			ePtStart,
			ePtEnd
		}
	}
}
